<template>
  <div class="pharmacy">

    <button  type="button" class="el-button el-button--text el-button--small"  v-if="!disabled" style="float: left; padding: 3px 0px;" @click="handleadd(0,'')"><!----><!----><span>添加用药史</span></button>
    <el-table
      :data="list"
      width="100%"

    >

      <el-table-column
        show-overflow-tooltip
        label="老人编号"
        prop="elderlyCode"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="diseasesName"
        label="疾病类型"

      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="drugName"
        label="药品名称"

      ></el-table-column>

      <el-table-column
        show-overflow-tooltip
        prop="dosage"
        label="用法用量"

      ></el-table-column>




      <el-table-column label="操作" prop="disabled" v-if="!disabled">
        <template slot-scope="scope">
          <el-button type="text"  @click="doDelete(scope.$index, scope.row)"  show-overflow-tooltip width="200px">删除<i class="el-icon-delete"></i></el-button>

        </template>


      </el-table-column>
    </el-table>
    <el-dialog
      title="添加用药史"
      :visible.sync="dialogFormVisible"
      width="500px"

    >

      <el-form ref="form" :model="form" label-width="80px" >
        <el-form-item label="疾病类型" >
          <el-select v-model="diseases"   placeholder="疾病类型"  >
            <el-option
              v-for="item in options"
              :key="item.diseases"
              :label="item.diseasesName"
              :value="item.diseases">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="用法用量" required>
          <el-input v-model="form.dosage"></el-input>
        </el-form-item>
        <el-form-item label="药品名称" required>
          <el-input v-model="form.drugName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save" >确 定</el-button>
      </div>


    </el-dialog>

  </div>
</template>

<script>
    import { searchdiseasesName } from "@/api/pharmacy"
    import { add } from "@/api/pharmacy"
    import { search } from "@/api/pharmacy"
    import { del } from "@/api/pharmacy"
    export default {
        name: "pharmacy",
        props: ["elderlyCode","dictionary","diseasess","disabled"],
        data(){
            return{
                diseases:"",
                options:{

                },
                list:[],
                form:{
                    diseases:"",
                    dosage:"",
                    drugName:"",
                    elderlyCode:"",

                },
                dialogFormVisible:false,
            }

        },
        created() {

              this.search()
        },
        updated(){

           this.diseasesss= this.diseasess
        },
        methods:{
            handleadd(){
                this.searchdiseasesName()
                this.diseases=""
                this.dialogFormVisible=true
            },

            search(){

                console.log(this.elderlyCode)
                if(this.elderlyCode){
                    let elderlyCode=this.elderlyCode
                    search({elderlyCode}).then(res =>{
                        this.list=res.data

                    })
                }else {

                }


            },

            searchdiseasesName(){

                if(this.elderlyCode){
                    let elderlyCode=this.elderlyCode
                    searchdiseasesName({elderlyCode}).then(res =>{
                        console.log(res)

                        this.options=res.data

                    })
                }else {

                }


            },
            save(){
                this.form.diseases=this.diseases
                this.form.elderlyCode=this.elderlyCode
               console.log(this.form)
                if(this.elderlyCode){
                    this.$refs["form"].validate(async (valid) => {
                        if (valid) {
                            if(this.form.diseases==""){
                                this.$message({
                                    message: "请选择疾病类型或请先添加其他病史",
                                    type: 'warning'
                                })
                            }else {
                                const {code} = await add(this.form);
                                console.log(code)
                                if(code==200){
                                    this.$message({
                                        message: "添加成功",
                                        type: 'success'
                                    })
                                    this.search()
                                }else {

                                }
                            }




                        } else {
                            return false;
                        }
                    });
                }else {
                    this.$message({
                        message: "请先添加基本信息",
                        type: 'warning'
                    })
                }
            },
            close(){
                this.dialogFormVisible=false
            },
            doDelete(index,row) {
                let elderlyCode=row.elderlyCode
                let diseases=row.diseases
                let drugName=row.drugName
                console.log(row)
                if (row.id) {
                    this.$baseConfirm("你确定要删除当前项吗", null, async () => {
                        const {code, msg } = await del(({elderlyCode,diseases,drugName}));
                        if(code==200){
                            this.$baseMessage("删除成功");
                            this.search()
                        }else {
                            this.$baseMessage("删除失败");
                        }
                    });
                }
            },
        },

    }
</script>

<style scoped>

</style>
